package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.BasicStandardRestrictedPart;
import cc.comac.mom.bean.QualityAsrCheckForm;
import cc.comac.mom.bean.QualityAsrFeaturesBean;
import cc.comac.mom.bean.QualityQarBasicStandard;
import cc.comac.mom.bean.QualityQarBasicStandardAsrFeatures;
import cc.comac.mom.bean.QualityQarBasicStandardCheck;
import cc.comac.mom.bean.QualityQarBasicStandardRequirement;
import cc.comac.mom.bean.QualityQarBasicStandardUpgrade;
import cc.comac.mom.bean.qar.QualityQarCheckInvoiceBean;
import cc.comac.mom.dao.entity.QualityQarBasicStandardAsrFeaturesDao;
import cc.comac.mom.dao.entity.QualityQarBasicStandardDao;
import cc.comac.mom.dao.entity.QualityQarBasicStandardUpgradeDao;
import cc.comac.mom.entity.QualityAsrCheckFormEntity;
import cc.comac.mom.entity.QualityAsrFeaturesEntity;
import cc.comac.mom.entity.QualityQarBasicStandardAsrFeaturesEntity;
import cc.comac.mom.entity.QualityQarBasicStandardCheckEntity;
import cc.comac.mom.entity.QualityQarBasicStandardEntity;
import cc.comac.mom.entity.QualityQarBasicStandardRequirementEntity;
import cc.comac.mom.entity.QualityQarBasicStandardUpgradeEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.DataStatus;
import cc.comac.mom.util.MomConstants;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class QualityQarBasicStandardUpgradeDataHandler
		extends ServiceImpl<QualityQarBasicStandardUpgradeDao, QualityQarBasicStandardUpgradeEntity> {
	@Autowired
	private QualityQarCheckInvoiceDataHandler qualityQarCheckInvoiceDataHandler;
	@Autowired
	private QualityQarBasicStandardUpgradeDao qualityQarBasicStandardUpgradeDao;
	@Autowired
	private QualityPclrTestInvoiceDataHandler qualityPclrTestInvoiceDataHandler;
	@Autowired
	private QualityAsrCheckFormDataHandler qualityAsrCheckFormDataHandler;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private QualityQarBasicStandardAsrFeaturesDataHandler qualityQarBasicStandardAsrFeaturesDataHandler;
	@Autowired
	private QualityQarBasicStandardCheckDataHandler qualityQarBasicStandardCheckDataHandler;

	@Autowired
	private QualityQarBasicStandardRequirementDataHandler qualityQarBasicStandardRequirementDataHandler;
	@Autowired
	private QualityQarBasicStandardDao qualityQarBasicStandardDao;
	@Autowired
	private QualityQarBasicStandardAsrFeaturesDao qualityQarBasicStandardAsrFeaturesDao;
	@Autowired
	private UniversalNumberGenerationDataHandler universalNumberGenerationDataHandler;
	@Autowired
	private QualityParamDataHandler qualityParamDataHandler;
	@Autowired
	public QualityQarBasicStandardDataHanldler qualityQarBasicStandardDataHanldler;

	@Autowired
	private BasicStandardRestrictedPartDataHandler basicStandardRestrictedPartDataHandler;
	// 母本升版保存
	public ResultBean insertQarBasicStandardUpgradeData(QualityQarBasicStandard qualityQarBasicStandard) {
		ResultBean resultBean = new ResultBean();
		QualityQarBasicStandardUpgrade qualityQarBasicStandardUpgrade = new QualityQarBasicStandardUpgrade();
		// 给母本升版数据赋值
		copyQarBasicStandardToUpgrade(qualityQarBasicStandard, qualityQarBasicStandardUpgrade);
		// 升版母本对应数据库字段
		QualityQarBasicStandardUpgradeEntity innerData = qualityQarBasicStandardUpgrade.getInnerData();

		List<QualityAsrFeaturesBean> asrFeaturesList = qualityQarBasicStandard.getQarBasicStandardAsrFeaturesList();
		// 将对象数据进行置换，方便保存特征库数据信息
		ArrayList<QualityQarBasicStandardAsrFeatures> qarBasicStandardAsrFeaturesList = new ArrayList<QualityQarBasicStandardAsrFeatures>();
		if (asrFeaturesList.size() > 0) {
			for (QualityAsrFeaturesBean qualityAsrFeaturesBean : asrFeaturesList) {
				QualityAsrFeaturesEntity innerData2 = qualityAsrFeaturesBean.getInnerData();
				QualityQarBasicStandardAsrFeatures qualityQarBasicStandardAsrFeatures = new QualityQarBasicStandardAsrFeatures();
				QualityQarBasicStandardAsrFeaturesEntity qualityQarBasicStandardAsrFeaturesEntity = new QualityQarBasicStandardAsrFeaturesEntity();
				qualityQarBasicStandardAsrFeatures.setInnerData(qualityQarBasicStandardAsrFeaturesEntity);
				qualityQarBasicStandardAsrFeatures.getInnerData().setAsrFeaturesId(innerData2.getId());
				baseEntityDataUtil.updateBaseEntityData(qualityQarBasicStandardAsrFeaturesEntity);
				qarBasicStandardAsrFeaturesList.add(qualityQarBasicStandardAsrFeatures);
			}
		}
		List<QualityQarBasicStandardCheck> qarBasicStandardCheckList = qualityQarBasicStandard
				.getQarBasicStandardCheckList();

		List<QualityQarBasicStandardRequirement> qarBasicStandardRequirementList = qualityQarBasicStandard
				.getQarBasicStandardRequirementList();
		
		List<BasicStandardRestrictedPart> restrictedPartList = qualityQarBasicStandard.getRestrictedPartList();

		if (qarBasicStandardCheckList.size() > 0) {
			innerData.setIfCheck("1");
		} else {
			innerData.setIfCheck("0");
		}
		int sum = 0;
		for (QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement : qarBasicStandardRequirementList) {
			if (qualityQarBasicStandardRequirement.getInnerData().getRequirementName().equals("0")) {
				sum = sum + 1;
			}
		}
		if (sum > 0) {
			innerData.setIfTest("1");
		} else {
			innerData.setIfTest("0");
		}
		innerData.setVersion(innerData.getVersion()+1L);
		// 新增母本升版数据
		baseEntityDataUtil.setBaseEntityData(innerData);
		qualityQarBasicStandardUpgradeDao.insert(innerData);
		long id = innerData.getId();

		if (qarBasicStandardAsrFeaturesList.size() > 0) {
			for (QualityQarBasicStandardAsrFeatures entity : qarBasicStandardAsrFeaturesList) {
				entity.getInnerData().setQarBasicStandardId(id);
			}
		}

		if (qarBasicStandardCheckList.size() > 0) {
			for (QualityQarBasicStandardCheck qualityQarBasicStandardCheck : qarBasicStandardCheckList) {
				qualityQarBasicStandardCheck.getInnerData().setQarBasicStandardId(id);
			}
		}

		if (qarBasicStandardRequirementList.size() > 0) {
			for (QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement : qarBasicStandardRequirementList) {
				qualityQarBasicStandardRequirement.getInnerData().setQarBasicStandardId(id);
			}
		}
		if (!CollectionUtils.isEmpty(restrictedPartList)) {
			for (BasicStandardRestrictedPart basicStandardRestrictedPart : restrictedPartList) {
				basicStandardRestrictedPart.getInnerData().setBasicStandardId(id);
			}
		}

		// 批量新增限售件数据
		if (restrictedPartList.size() > 0) {
			basicStandardRestrictedPartDataHandler.batchInsertData(restrictedPartList);
		}
		// 批量新增asr特征库对应数据
		qualityQarBasicStandardAsrFeaturesDataHandler
				.batchInsertQarBasicStandardAsrFeatures(qarBasicStandardAsrFeaturesList);

		// 批量新增qar母本检验/验证特性
		qualityQarBasicStandardCheckDataHandler.batchInsertQarBasicStandardCheck(qarBasicStandardCheckList);

		// 批量新增qar母本对应的检验/限制要求
		qualityQarBasicStandardRequirementDataHandler
				.batchInsertQarBasicStandardRequirement(qarBasicStandardRequirementList);

		return resultBean;
	}

	// 将母本数据复制到母本的升版表中
	public void copyQarBasicStandardToUpgrade(QualityQarBasicStandard qualityQarBasicStandard,
			QualityQarBasicStandardUpgrade qualityQarBasicStandardUpgrade) {
		QualityQarBasicStandardEntity innerData = qualityQarBasicStandard.getInnerData();
		QualityQarBasicStandardUpgradeEntity qualityQarBasicStandardUpgradeEntity = new QualityQarBasicStandardUpgradeEntity();
		qualityQarBasicStandardUpgradeEntity.setAtpNumber(innerData.getAtpNumber());
		qualityQarBasicStandardUpgradeEntity.setAtpVersion(innerData.getAtpVersion());
		qualityQarBasicStandardUpgradeEntity.setPartNumber(innerData.getPartVersion());
		qualityQarBasicStandardUpgradeEntity.setCategory(innerData.getCategory());
		qualityQarBasicStandardUpgradeEntity
				.setChangedLetters(qualityQarBasicStandardUpgradeEntity.getChangedLetters());
		qualityQarBasicStandardUpgradeEntity.setChangeNumber(innerData.getChangeNumber());
		qualityQarBasicStandardUpgradeEntity.setColorCode(innerData.getColorCode());
		qualityQarBasicStandardUpgradeEntity.setDrawingNumber(innerData.getDrawingNumber());
		qualityQarBasicStandardUpgradeEntity.setDrawingRevision(innerData.getDrawingRevision());
		qualityQarBasicStandardUpgradeEntity.setIfCheck(innerData.getIfCheck());
		qualityQarBasicStandardUpgradeEntity.setIfCorrectEntrust(innerData.getIfCorrectEntrust());
		qualityQarBasicStandardUpgradeEntity.setIfRestrictedPart(innerData.getIfRestrictedPart());
		qualityQarBasicStandardUpgradeEntity.setIfSampling(innerData.getIfSampling());
		qualityQarBasicStandardUpgradeEntity.setIfTest(innerData.getIfTest());
		qualityQarBasicStandardUpgradeEntity.setLevel(innerData.getLevel());
		qualityQarBasicStandardUpgradeEntity.setManufacturer(innerData.getManufacturer());
		qualityQarBasicStandardUpgradeEntity.setMaterialNumber(innerData.getMaterialNumber());
		qualityQarBasicStandardUpgradeEntity.setStatus(innerData.getStatus());
        qualityQarBasicStandardUpgradeEntity.setStandard(innerData.getStandard());
        qualityQarBasicStandardUpgradeEntity.setStandardVersion(innerData.getStandardVersion());
        qualityQarBasicStandardUpgradeEntity.setRestrictedPartRequirements(innerData.getRestrictedPartRequirements());
		qualityQarBasicStandardUpgradeEntity.setProjectRequirements(innerData.getProjectRequirements());
		qualityQarBasicStandardUpgradeEntity.setRestrictedPartStorageTime(innerData.getRestrictedPartStorageTime());
        qualityQarBasicStandardUpgradeEntity.setChangedLetters(innerData.getChangedLetters());
        qualityQarBasicStandardUpgradeEntity.setRouteMaterialNumber(innerData.getRouteMaterialNumber());
		qualityQarBasicStandardUpgradeEntity.setMaterialScience(innerData.getMaterialScience());
		qualityQarBasicStandardUpgradeEntity.setMaterialType(innerData.getMaterialType());
		qualityQarBasicStandardUpgradeEntity.setMatNumber(innerData.getMatNumber());
		qualityQarBasicStandardUpgradeEntity.setModel(innerData.getModel());
		qualityQarBasicStandardUpgradeEntity.setNorms(innerData.getNorms());
		qualityQarBasicStandardUpgradeEntity.setPartName(innerData.getPartName());
		qualityQarBasicStandardUpgradeEntity.setPartNumber(innerData.getPartNumber());
		qualityQarBasicStandardUpgradeEntity.setPerformanceRequirement(innerData.getPerformanceRequirement());
		qualityQarBasicStandardUpgradeEntity.setPlanRemark(innerData.getPlanRemark());
		qualityQarBasicStandardUpgradeEntity.setQarBasicStandardId(innerData.getId());
		qualityQarBasicStandardUpgradeEntity.setQarBasicStandardNumber(innerData.getQarBasicStandardNumber());
		qualityQarBasicStandardUpgradeEntity.setRemark(innerData.getRemark());
		qualityQarBasicStandardUpgradeEntity.setRequiredSize(innerData.getRequiredSize());
		qualityQarBasicStandardUpgradeEntity.setRestrictedPartControlMode(innerData.getRestrictedPartControlMode());
		qualityQarBasicStandardUpgradeEntity
				.setRouteMaterialNumber(qualityQarBasicStandardUpgradeEntity.getRouteMaterialNumber());
		qualityQarBasicStandardUpgradeEntity.setSamplesQuantity(innerData.getSamplesQuantity());
		qualityQarBasicStandardUpgradeEntity.setSamplesSize(innerData.getSamplesSize());
		qualityQarBasicStandardUpgradeEntity.setShape(innerData.getShape());
		qualityQarBasicStandardUpgradeEntity.setSoftwareNumber(innerData.getSoftwareNumber());
		qualityQarBasicStandardUpgradeEntity.setSoftwareRevision(innerData.getSoftwareRevision());
		qualityQarBasicStandardUpgradeEntity.setTestItemQuantity(innerData.getTestItemQuantity());
		qualityQarBasicStandardUpgradeEntity.setType(innerData.getType());
		qualityQarBasicStandardUpgradeEntity.setVersion(innerData.getVersion());
		qualityQarBasicStandardUpgradeEntity.setIfChildRestrictedPart(innerData.getIfChildRestrictedPart());
		qualityQarBasicStandardUpgradeEntity.setRestrictedPartNo(innerData.getRestrictedPartNo());
		qualityQarBasicStandardUpgradeEntity.setRestrictedPartProductNo(innerData.getRestrictedPartProductNo());
		qualityQarBasicStandardUpgradeEntity.setCompositionStyle(innerData.getCompositionStyle());
		qualityQarBasicStandardUpgrade.setInnerData(qualityQarBasicStandardUpgradeEntity);
	}
	
	/**
	 * 将母本的升版表中数据复制到母本
	 * **/
		public void copyQarUpgradeToBasicStandard(QualityQarBasicStandardUpgrade qualityQarBasicStandardUpgrade,
				QualityQarBasicStandard qualityQarBasicStandard) {
			QualityQarBasicStandardUpgradeEntity innerData = qualityQarBasicStandardUpgrade.getInnerData();
			QualityQarBasicStandardEntity qualityQarBasicStandardEntity = new QualityQarBasicStandardEntity();
			qualityQarBasicStandardEntity.setAtpNumber(innerData.getAtpNumber());
			qualityQarBasicStandardEntity.setAtpVersion(innerData.getAtpVersion());
			qualityQarBasicStandardEntity.setPartNumber(innerData.getPartVersion());
			qualityQarBasicStandardEntity.setCategory(innerData.getCategory());
			qualityQarBasicStandardEntity.setChangedLetters(qualityQarBasicStandardEntity.getChangedLetters());
			qualityQarBasicStandardEntity.setChangeNumber(innerData.getChangeNumber());
			qualityQarBasicStandardEntity.setColorCode(innerData.getColorCode());
			qualityQarBasicStandardEntity.setDrawingNumber(innerData.getDrawingNumber());
			qualityQarBasicStandardEntity.setDrawingRevision(innerData.getDrawingRevision());
			qualityQarBasicStandardEntity.setIfCheck(innerData.getIfCheck());
			qualityQarBasicStandardEntity.setIfCorrectEntrust(innerData.getIfCorrectEntrust());
			qualityQarBasicStandardEntity.setIfRestrictedPart(innerData.getIfRestrictedPart());
			qualityQarBasicStandardEntity.setIfSampling(innerData.getIfSampling());
			qualityQarBasicStandardEntity.setIfTest(innerData.getIfTest());
			qualityQarBasicStandardEntity.setLevel(innerData.getLevel());
			qualityQarBasicStandardEntity.setManufacturer(innerData.getManufacturer());
			qualityQarBasicStandardEntity.setMaterialNumber(innerData.getMaterialNumber());
			qualityQarBasicStandardEntity.setMaterialScience(innerData.getMaterialScience());
			qualityQarBasicStandardEntity.setMaterialType(innerData.getMaterialType());
			qualityQarBasicStandardEntity.setMatNumber(innerData.getMatNumber());
			qualityQarBasicStandardEntity.setModel(innerData.getModel());
			qualityQarBasicStandardEntity.setNorms(innerData.getNorms());
			qualityQarBasicStandardEntity.setPartName(innerData.getPartName());
			qualityQarBasicStandardEntity.setPartNumber(innerData.getPartNumber());
			qualityQarBasicStandardEntity.setStatus(innerData.getStatus());
			qualityQarBasicStandardEntity.setStandard(innerData.getStandard());
			qualityQarBasicStandardEntity.setStandardVersion(innerData.getStandardVersion());
			qualityQarBasicStandardEntity.setProjectRequirements(innerData.getProjectRequirements());
			qualityQarBasicStandardEntity.setPerformanceRequirement(innerData.getPerformanceRequirement());
			qualityQarBasicStandardEntity.setPlanRemark(innerData.getPlanRemark());
			qualityQarBasicStandardEntity.setId(innerData.getQarBasicStandardId());
			qualityQarBasicStandardEntity.setQarBasicStandardNumber(innerData.getQarBasicStandardNumber());
			qualityQarBasicStandardEntity.setRemark(innerData.getRemark());
			qualityQarBasicStandardEntity.setRequiredSize(innerData.getRequiredSize());
			qualityQarBasicStandardEntity.setRestrictedPartControlMode(innerData.getRestrictedPartControlMode());
			qualityQarBasicStandardEntity
					.setRouteMaterialNumber(qualityQarBasicStandardEntity.getRouteMaterialNumber());
			qualityQarBasicStandardEntity.setSamplesQuantity(innerData.getSamplesQuantity());
			qualityQarBasicStandardEntity.setSamplesSize(innerData.getSamplesSize());
			qualityQarBasicStandardEntity.setShape(innerData.getShape());
			qualityQarBasicStandardEntity.setSoftwareNumber(innerData.getSoftwareNumber());
			qualityQarBasicStandardEntity.setSoftwareRevision(innerData.getSoftwareRevision());
			qualityQarBasicStandardEntity.setTestItemQuantity(innerData.getTestItemQuantity());
			qualityQarBasicStandardEntity.setType(innerData.getType());
			qualityQarBasicStandardEntity.setVersion(innerData.getVersion());
			qualityQarBasicStandard.setInnerData(qualityQarBasicStandardEntity);
		}
	

	// 母本升版提交
	public ResultBean submitQarBasicStandardUpgradeData(QualityQarBasicStandard qualityQarBasicStandard) {
		ResultBean resultBean = new ResultBean(true);
		QualityQarBasicStandardEntity innerData = qualityQarBasicStandard.getInnerData();
		// 通过条件查询qar母本对应数据
		QueryWrapper<QualityQarBasicStandardEntity> queryWrapper1 = new QueryWrapper<QualityQarBasicStandardEntity>();
		queryWrapper1.eq("row_status", 1);
		queryWrapper1.eq("qar_basic_standard_id", innerData.getId());
		QualityQarBasicStandardEntity selectOne2 = qualityQarBasicStandardDao.selectOne(queryWrapper1);
		innerData.setId(selectOne2.getId());
		// 查询母本升版对应数据
		QueryWrapper<QualityQarBasicStandardUpgradeEntity> queryWrapper2 = new QueryWrapper<QualityQarBasicStandardUpgradeEntity>();
		queryWrapper2.eq("row_status", 1);
		queryWrapper2.eq("qar_basic_standard_id", innerData.getId());
		QualityQarBasicStandardUpgradeEntity selectOne3 = qualityQarBasicStandardUpgradeDao.selectOne(queryWrapper2);
		innerData.setVersion(selectOne3.getVersion());
		// 物理删除升版过程中的数据检验通过升版主键
		qualityQarBasicStandardCheckDataHandler.removeDataByQarBasicStandardUpgradeId(selectOne3.getId());


		// 物理删除升版过程中的数据实验通过升版主键
		qualityQarBasicStandardRequirementDataHandler.removeByQarBasicStandardId(selectOne3.getId());

		// 物理删除升版过程中的asr特征库检验数据
		List<Long> fIds=new ArrayList<>();
		fIds.add(selectOne3.getId());
		qualityQarBasicStandardAsrFeaturesDataHandler.removeDataByQarBasicStandardIds(fIds);
		// 把原来的数据改成升级母本的id
		removeOterData(selectOne2.getId(),selectOne3.getId());

		//把母本表和升级母本表数据互换
		QualityQarBasicStandard q=new QualityQarBasicStandard();
		q.setInnerData(selectOne2);
		QualityQarBasicStandardUpgrade u=new QualityQarBasicStandardUpgrade();
		this.copyQarBasicStandardToUpgrade(q,u);
        //修改升级数据
		u.getInnerData().setId(selectOne3.getId());
		u.getInnerData().setQarBasicStandardId(selectOne3.getQarBasicStandardId());
		qualityQarBasicStandardUpgradeDao.updateById(u.getInnerData());
		qualityQarBasicStandardDao.updateById(innerData);
		
		List<QualityAsrFeaturesBean> qarBasicStandardAsrFeaturesList = qualityQarBasicStandard
				.getQarBasicStandardAsrFeaturesList();
		List<QualityQarBasicStandardAsrFeatures> beanList = new ArrayList<QualityQarBasicStandardAsrFeatures>();
		if (qarBasicStandardAsrFeaturesList.size() > 0) {
			for (QualityAsrFeaturesBean qualityAsrFeaturesBean : qarBasicStandardAsrFeaturesList) {
				QualityAsrFeaturesEntity innerData2 = qualityAsrFeaturesBean.getInnerData();
				QualityQarBasicStandardAsrFeatures qualityQarBasicStandardAsrFeatures = new QualityQarBasicStandardAsrFeatures();
				QualityQarBasicStandardAsrFeaturesEntity qualityQarBasicStandardAsrFeaturesEntity = new QualityQarBasicStandardAsrFeaturesEntity();
				qualityQarBasicStandardAsrFeatures.setInnerData(qualityQarBasicStandardAsrFeaturesEntity);
				qualityQarBasicStandardAsrFeaturesEntity.setAsrFeaturesId(innerData2.getId());
				qualityQarBasicStandardAsrFeaturesEntity.setQarBasicStandardId(selectOne2.getId());
				beanList.add(qualityQarBasicStandardAsrFeatures);
			}
		}
		// 更新对应asr特征库对应的数据
		qualityQarBasicStandardAsrFeaturesDataHandler.addOrUpdateDataList(beanList);

		
		// 更新对应实验数据
		List<QualityQarBasicStandardRequirement> qarBasicStandardRequirementList = qualityQarBasicStandard
				.getQarBasicStandardRequirementList();
		if (qarBasicStandardRequirementList.size() > 0) {
			// 将有可能和升版有关的数据主键强行同步到母本表中
			for (QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement : qarBasicStandardRequirementList) {
				qualityQarBasicStandardRequirement.getInnerData().setQarBasicStandardId(selectOne2.getId());
			}
			qualityQarBasicStandardRequirementDataHandler
					.addOrUpdateDatas(qualityQarBasicStandard.getQarBasicStandardRequirementList());

		}

		// 更新对应检验数据
		List<QualityQarBasicStandardCheck> qarBasicStandardCheckList = qualityQarBasicStandard
				.getQarBasicStandardCheckList();
		if (qarBasicStandardCheckList.size() > 0) {
			for (QualityQarBasicStandardCheck qualityQarBasicStandardCheck : qarBasicStandardCheckList) {
				qualityQarBasicStandardCheck.getInnerData().setQarBasicStandardId(selectOne2.getId());
			}
		}
		qualityQarBasicStandardCheckDataHandler.batchInsertQarBasicStandardCheck(qarBasicStandardCheckList);

		// 当提交数据时先校验模板升版时表中是否存在对应数据，存在软删除
		QueryWrapper<QualityQarBasicStandardUpgradeEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardUpgradeEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("qar_basic_standard_id", innerData.getId());
		QualityQarBasicStandardUpgradeEntity selectOne = qualityQarBasicStandardUpgradeDao.selectOne(queryWrapper);
		if (selectOne != null) {
			selectOne.setRowStatus(0);
			qualityQarBasicStandardUpgradeDao.updateById(selectOne);
		}

        //如果母本被改成需要检验单了，生成检验单
		if("1".equals(innerData.getIfSampling())){
			List<Long> ids=qualityQarBasicStandardDataHanldler.freezeAllAsrCheckForm(innerData.getId(),"0");
			//把母本的特征数据同步到对应的检验单中
			qualityAsrCheckFormDataHandler.batchsyscAsrCheckFormDetailInfo(ids, beanList,innerData.getId());
			List<QualityQarCheckInvoiceBean> qarCheckList=
					qualityAsrCheckFormDataHandler.selectQarCheckInvoiceInfo(innerData.getId());
			if(qarCheckList!=null&&qarCheckList.size()>0) {
				List<QualityAsrCheckForm> asrFormList = new ArrayList<>();
				String model = innerData.getModel();
				for (QualityQarCheckInvoiceBean invoice : qarCheckList) {
					// 生成QAR编号
					String qarNo = invoice.getQarNo();
					String asrNo = qarNo + "-ASR";
					// 预留生成ASR检验单
					QualityAsrCheckForm qualityAsrCheckForm = new QualityAsrCheckForm();
					QualityAsrCheckFormEntity qualityAsrCheckFormEntity = new QualityAsrCheckFormEntity();
					qualityAsrCheckFormEntity.setAsrNumber(asrNo);
					qualityAsrCheckFormEntity.setQarNumber(qarNo);
					qualityAsrCheckFormEntity.setPartNumber(invoice.getPartNo());
					qualityAsrCheckFormEntity.setFormType(invoice.getFormType());
					qualityAsrCheckFormEntity.setProductModel(model);
					qualityAsrCheckFormEntity.setPartType(invoice.getMaterialType());
					qualityAsrCheckFormEntity.setFoPoNumber(invoice.getPurchaseOrder());
					qualityAsrCheckFormEntity.setSupplierWorkshop(invoice.getVendorName());
					qualityAsrCheckFormEntity.setPlanRemark(innerData.getPlanRemark());
					qualityAsrCheckFormEntity.setStatus(DataStatus.planning);
					qualityAsrCheckForm.setInnerData(qualityAsrCheckFormEntity);
					asrFormList.add(qualityAsrCheckForm);
				}
				if (asrFormList != null && asrFormList.size() > 0) {
					qualityAsrCheckFormDataHandler.batchInsertAsrCheckForm(asrFormList);
				}
			}
		}
		//如果母本被改成需要检验单了，生成检验单
		if("1".equals(innerData.getIfTest())){
			qualityPclrTestInvoiceDataHandler.initPclrTestInvoice(qarBasicStandardRequirementList,innerData);
		}
		//解冻qar
		qualityQarCheckInvoiceDataHandler.updateCurrentStatus(innerData.getId(), MomConstants.QUALITY_QAR_CURRENT_STATUS_FROZEN);
		// 升版同步qar数据
		qualityQarCheckInvoiceDataHandler.upgradeUpdateQarData(qualityQarBasicStandard.getQarBasicStandardCheckList(),qarBasicStandardRequirementList,innerData.getId(),innerData);
		
		// 同步升版母本的数据
		basicStandardRestrictedPartDataHandler.submitBasicStandardUpdateData(qualityQarBasicStandard.getRestrictedPartList(), selectOne2.getId(), selectOne3.getId());
		// 同步升版的限寿数据到母本中
		basicStandardRestrictedPartDataHandler.updateQarCheckDataByRestrictedPart(qualityQarBasicStandard.getRestrictedPartList(), selectOne2.getId());
		
		return resultBean;
	}

	public void removeOterData(Long mId,Long fId) {
		QualityQarBasicStandard qualityQarBasicStandard =new QualityQarBasicStandard();
		qualityQarBasicStandard.setInnerData( new QualityQarBasicStandardEntity());
		qualityQarBasicStandard.getInnerData().setId(mId);
		qualityQarBasicStandard= (QualityQarBasicStandard) qualityQarBasicStandardDataHanldler.selectByQarBasicStandardId(qualityQarBasicStandard,false).getObject();

		this.updateChildrenList(qualityQarBasicStandard,fId);
		// 物理删除升版过程中的数据检验通过升版主键
		//qualityQarBasicStandardCheckDataHandler.removeDataByQarBasicStandardUpgradeId(ids);


		// 物理删除升版过程中的数据实验通过升版主键
		//qualityQarBasicStandardRequirementDataHandler.removeByQarBasicStandardId(ids);

		// 物理删除升版过程中的asr特征库检验数据
		//qualityQarBasicStandardAsrFeaturesDataHandler.removeDataByQarBasicStandardIds(ids);
	}

    public void updateChildrenList(QualityQarBasicStandard bean,Long id){
        List<QualityQarBasicStandardCheck> qarBasicStandardCheckList = bean
                .getQarBasicStandardCheckList();

        List<QualityQarBasicStandardRequirement> qarBasicStandardRequirementList = bean
                .getQarBasicStandardRequirementList();
        /*List<QualityAsrFeaturesBean> asrFeaturesList = bean.getQarBasicStandardAsrFeaturesList();
        // 将对象数据进行置换，方便保存特征库数据信息
        ArrayList<QualityQarBasicStandardAsrFeatures> qarBasicStandardAsrFeaturesList = new ArrayList<QualityQarBasicStandardAsrFeatures>();
        if (asrFeaturesList.size() > 0) {
            for (QualityAsrFeaturesBean qualityAsrFeaturesBean : asrFeaturesList) {
                QualityAsrFeaturesEntity innerData2 = qualityAsrFeaturesBean.getInnerData();
                QualityQarBasicStandardAsrFeatures qualityQarBasicStandardAsrFeatures = new QualityQarBasicStandardAsrFeatures();
                QualityQarBasicStandardAsrFeaturesEntity qualityQarBasicStandardAsrFeaturesEntity = new QualityQarBasicStandardAsrFeaturesEntity();
                qualityQarBasicStandardAsrFeatures.setInnerData(qualityQarBasicStandardAsrFeaturesEntity);
                qualityQarBasicStandardAsrFeatures.getInnerData().setAsrFeaturesId(innerData2.getId());
				qualityQarBasicStandardAsrFeatures.getInnerData().setQarBasicStandardId(innerData2.getQa());
				qualityQarBasicStandardAsrFeatures.getInnerData().setAsrFeaturesId(innerData2.getId());
                baseEntityDataUtil.updateBaseEntityData(qualityQarBasicStandardAsrFeaturesEntity);
                qarBasicStandardAsrFeaturesList.add(qualityQarBasicStandardAsrFeatures);
            }
        }*/
		// 通过qar母本主键获取对应的特征库数据对应的主键集合
		QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardAsrFeaturesEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("qar_basic_standard_id", bean.getInnerData().getId());
		List<QualityQarBasicStandardAsrFeaturesEntity> asrList = qualityQarBasicStandardAsrFeaturesDao.selectList(queryWrapper);

        if (asrList!=null&&asrList.size() > 0) {
            for (QualityQarBasicStandardAsrFeaturesEntity entity : asrList) {
				entity.setQarBasicStandardId(id);
            }
        }
        List<QualityQarBasicStandardCheckEntity> listStandard=new ArrayList<>();
        if (qarBasicStandardCheckList.size() > 0) {
            for (QualityQarBasicStandardCheck qualityQarBasicStandardCheck : qarBasicStandardCheckList) {
                qualityQarBasicStandardCheck.getInnerData().setQarBasicStandardId(id);
                if(qualityQarBasicStandardCheck.getQarBasicStandardCheckList()!=null&&qualityQarBasicStandardCheck.getQarBasicStandardCheckList().size()>0){
                	for(QualityQarBasicStandardCheckEntity detail:qualityQarBasicStandardCheck.getQarBasicStandardCheckList()){
						detail.setQarBasicStandardId(id);
						listStandard.add(detail);
					}
				}
                listStandard.add(qualityQarBasicStandardCheck.getInnerData());
            }
        }
        List<QualityQarBasicStandardRequirementEntity> listQuir=new ArrayList<>();
        if (qarBasicStandardRequirementList.size() > 0) {
            for (QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement : qarBasicStandardRequirementList) {
                qualityQarBasicStandardRequirement.getInnerData().setQarBasicStandardId(id);
                listQuir.add(qualityQarBasicStandardRequirement.getInnerData());
            }
        }

        //修改asr特征库对应数据
		if(asrList!=null&&asrList.size()>0) {
			qualityQarBasicStandardAsrFeaturesDataHandler
					.updateBatchById(asrList);
		}

        // 修改qar母本检验/验证特性
		if(listStandard!=null&&listStandard.size()>0) {
			qualityQarBasicStandardCheckDataHandler.updateBatchById(listStandard);
		}

        // 修改qar母本对应的检验/限制要求
		if(listQuir!=null&&listQuir.size()>0) {
			qualityQarBasicStandardRequirementDataHandler
					.updateBatchById(listQuir);
		}
    }
}
